package com.electric.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.electric.common.LoggerInterface;
import com.electric.entity.CustomRepairRecord;
import com.electric.entity.RepairRecord;
import com.electric.mapper.RepairRecordMapper;
import com.electric.service.IRepairRecordService;

/**
 * <p>
 * 故障记录 服务实现类
 * </p>
 *
 * @author zhangbin
 * @since 2018-08-28
 */
@Service
public class RepairRecordServiceImpl extends ServiceImpl<RepairRecordMapper, RepairRecord> implements IRepairRecordService,LoggerInterface {
	@Autowired
	private RepairRecordMapper recordRecordMapper;
	
	@Override
	public Page<CustomRepairRecord> selectPage(int pageNum, int pageSize, String customerName, String repairType,
			String voltageLevel, Date startDate, Date endDate, Date failureDateStart, Date failureDateEnd) {
		Page<CustomRepairRecord> page = new Page<CustomRepairRecord>(pageNum, pageSize);
		
		Map<String,Object> param = new HashMap<String,Object>();
		
		if(StringUtils.isNotBlank(customerName)) {
			param.put("customerName", customerName);
		}
		
		if(StringUtils.isNotBlank(repairType)) {
			param.put("repairType", repairType);
		}
		
		if(StringUtils.isNotBlank(voltageLevel)) {
			param.put("voltageLevel", voltageLevel);
		}
		
		if(null != startDate) {
			param.put("startDate", startDate);
		}
		
		if(null != endDate) {
			param.put("endDate", endDate);
		}
		if(null != failureDateStart) {
			param.put("failureDateStart", failureDateStart);
		}
		
		if(null != failureDateEnd) {
			param.put("failureDateEnd", failureDateEnd);
		}
		
		try {
			return page.setRecords(recordRecordMapper.selectListPage(page, param));
		} catch(Exception e) {
			logger.error(e.getMessage(),e);
		}
		return page;
	}

	@Override
	public CustomRepairRecord queryCustomRepairRecord(String seqId) {
		if(StringUtils.isNotBlank(seqId)) 
			return recordRecordMapper.queryCustomRepairRecord(seqId);
		else 
			return null;
	}


	public boolean checkExistsByCustomer(String customerSeqId) {
		EntityWrapper<RepairRecord> wrapper = new EntityWrapper<RepairRecord>();
		
		wrapper.eq("customer_number",customerSeqId);
		
		int selectCount = selectCount(wrapper);
		
		return selectCount > 0;
	}

	public boolean cleanDataByCustomer(String customerSeqId) {
		try {
			EntityWrapper<RepairRecord> wrapper = new EntityWrapper<RepairRecord>();
			
			wrapper.eq("customer_number",customerSeqId);
			
			delete(wrapper);
			
			return true;
		} catch(Exception e) {
			logger.error(e.getMessage(),e);
		}
		return false;
	}
	
	public boolean moveDataByCustomer(String customerSeqId, String newCustomerSeqId) {
		try {
			RepairRecord entity = new RepairRecord();
			entity.setCustomerNumber(newCustomerSeqId);
			
			EntityWrapper<RepairRecord> wrapper = new EntityWrapper<RepairRecord>();
			
			wrapper.eq("customer_number",customerSeqId);
			
			update(entity, wrapper);
			return true;
		} catch(Exception e) {
			logger.error(e.getMessage(),e);
		}
		return false;
	}

	public boolean checkExistsByRepairType(String repairTypeSeqId) {
		EntityWrapper<RepairRecord> wrapper = new EntityWrapper<RepairRecord>();
		
		wrapper.eq("repair_type",repairTypeSeqId);
		
		int selectCount = selectCount(wrapper);
		
		return selectCount > 0;
	}

	public boolean cleanDataByRepairType(String repairTypeSeqId) {
		try {
			EntityWrapper<RepairRecord> wrapper = new EntityWrapper<RepairRecord>();
			
			wrapper.eq("repair_type",repairTypeSeqId);
			
			delete(wrapper);
			
			return true;
		} catch(Exception e) {
			logger.error(e.getMessage(),e);
		}
		return false;
	}

	public boolean moveDataByRepairType(String repairTypeSeqId, String newRepairTypeSeqId) {
		try {
			RepairRecord entity = new RepairRecord();
			entity.setRepairType(newRepairTypeSeqId);
			EntityWrapper<RepairRecord> wrapper = new EntityWrapper<RepairRecord>();
			
			wrapper.eq("repair_type",repairTypeSeqId);
			
			update(entity, wrapper);
			
			return true;
		} catch(Exception e) {
			logger.error(e.getMessage(),e);
		}
		return false;
	}

	public boolean checkExistsByVoltageLevel(String voltageLevelSeqId) {
		EntityWrapper<RepairRecord> wrapper = new EntityWrapper<RepairRecord>();
		
		wrapper.eq("voltage_level",voltageLevelSeqId);
		
		int selectCount = selectCount(wrapper);
		
		return selectCount > 0;
	}

	public boolean cleanDataByVoltageLevel(String voltageLevelSeqId) {
		try {
			EntityWrapper<RepairRecord> wrapper = new EntityWrapper<RepairRecord>();
			
			wrapper.eq("voltage_level",voltageLevelSeqId);
			
			delete(wrapper);
			
			return true;
		} catch(Exception e) {
			logger.error(e.getMessage(),e);
		}
		return false;
	}

	public boolean moveDataByVoltageLevel(String voltageLevelSeqId, String newVoltageLevelSeqId) {
		try {
			RepairRecord entity = new RepairRecord();
			entity.setVoltageLevel(newVoltageLevelSeqId);
			
			EntityWrapper<RepairRecord> wrapper = new EntityWrapper<RepairRecord>();
			
			wrapper.eq("voltage_level",voltageLevelSeqId);
			
			update(entity, wrapper);
			
			return true;
		} catch(Exception e) {
			logger.error(e.getMessage(),e);
		}
		return false;
	}
}
